subroutine rtraj(step)
use var
use type
implicit none
integer  :: ss,step,y,ioerr = 0, iout, iout2, i, mm, step_l
character*20 :: string
real, allocatable :: qsingle(:,:)

allocate (qsingle(6,tnodes))
write(*,*)"READ THE TRAJ FILE"
Escale = Elastic_L1 * nematic_l * 1.0d-9 ! (J/m)*(nm)*(10^-9)
iout = 200 + 1
iout2 = 300 + 1
mm = int(traj(1)/sample)

do
   read(iout,IOSTAT=ioerr)string
   if (ioerr == 0) then
      read(iout)frame,ss,step,y
      read(iout)ISEED  
      read(iout)trj_E
      read(iout)NP_position
      read(iout)qsingle
      if (frame == 0) then
         write(iout2,'(I8,2X,4(f15.6),2(f20.6))')step,trj_E(:,1),Escale*trj_E(5,1)/kbT
      elseif (step == 0) then
         Do i=1,mm
            if (trj_E(1,i)/=0) then
               write(iout2,'(I8,2X,4(f15.6),2(f20.6))')step_l+i*sample,trj_E(:,i),Escale*trj_E(5,i)/kbT
            else
               step = step_l+(i-1)*sample
               exit
            endif
         enddo
      else
         Do i=1,mm
            if (trj_E(1,i)/=0) then
               write(iout2,'(I8,2X,4(f15.6),2(f20.6))')step_l+i*sample,trj_E(:,i),Escale*trj_E(5,i)/kbT
            else
               step = step_l+(i-1)*sample
               exit
            endif
         enddo
      endif
   else
      exit
   endif
   step_l = step
   trj_E = 0.d0
enddo

q = qsingle
deallocate(qsingle)
write(*,*)"FRAME",frame,"CYCLE",ss,"STEP",step
write(222,*)"READ THE TRAJ FILE"
write(222,*)"FRAME",frame,"CYCLE",ss,"STEP",step
call RANDOM_SEED
call RANDOM_SEED(put = iseed(1:2))

frame = frame + 1
end subroutine

